package com.ddwl.goods.controller.bargain;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.goods.service.bargain.BargainOrderService;
import com.ddwl.goods.service.bargain.BargainService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.bargain.BargainOrderBo;
import com.ddwl.schema.bo.goods.bargain.BargainOrderPageBo;
import com.ddwl.schema.bo.goods.bargain.MyBargainOrderPageBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.bargain.BargainLogVo;
import com.ddwl.schema.vo.goods.bargain.BargainOrderDetailVo;
import com.ddwl.schema.vo.goods.bargain.BargainOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @Author hemiaojun
 * @Date 2020/10/26 9:42
 */
@RestController
@RequestMapping("/bargainOrder")
@Api(tags = "用户砍价订单")
public class BargainOrderController extends BaseController {

    @Resource
    private BargainOrderService bargainOrderService;
    @Resource
    private BargainService bargainService;


    @PostMapping("/free/{activityId}")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO})
    @ApiOperation("免费拿")
    public RestVo<BargainLogVo>free(@PathVariable("activityId") @ApiParam("活动id") String activityId, @RequestParam @ApiParam("sku编码 ")String barcode,@RequestParam String addrId){
        BargainOrderBo bo=new BargainOrderBo();
        bo.setActivityId(activityId);
        bo.setBarcode(barcode);
        bo.setAddrId(addrId);
        bo.setNickname(getUserNameNew());
        bo.setUserId(getUserId());
        bo.setDeliveryType(0);
        return RestVo.ok(bargainService.free(bo));
    }

    @PostMapping("/wbm/free")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("五八马小程序免费拿")
    public RestVo<BargainLogVo>wbmFree(@Valid @RequestBody BargainOrderBo bo){
        bo.setNickname(getUserNameNew());
        bo.setUserId(getUserId());
        return RestVo.ok(bargainService.free(bo));
    }

    @PostMapping("/myList")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("我的砍价列表")
    public RestVo<PageVo<BargainOrderVo>> myList(@Valid @RequestBody PageBo<MyBargainOrderPageBo> bo){
        bo.getParam().setUserId(getUserId());
        return RestVo.ok(bargainOrderService.myList(bo));
    }

    @GetMapping("/detail/{bargainOrderId}")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("砍价单详情")
    public RestVo<BargainOrderDetailVo> detail(@PathVariable("bargainOrderId") String bargainOrderId){
        return RestVo.ok(bargainOrderService.detail(bargainOrderId));
    }

    @GetMapping("/share/detail/{bargainOrderId}")
    @ApiOperation("分享砍价单详情")
    public RestVo<BargainOrderDetailVo> shareDetail(@PathVariable("bargainOrderId") String bargainOrderId){
        return RestVo.ok(bargainOrderService.detail(bargainOrderId));
    }

    @PostMapping("/page")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    @ApiOperation("后台砍价记录分页")
    public RestVo<PageVo<BargainOrderVo>> page(@RequestBody PageBo<BargainOrderPageBo> bo){
        return RestVo.ok(bargainOrderService.page(bo));
    }

    @PostMapping("/buy/{bargainOrderId}")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("下单")
    public RestVo<String> buy(@PathVariable("bargainOrderId") String bargainOrderId){
        String orderId=bargainService.buy(bargainOrderId,getUserId(),getProduct());
        return RestVo.ok(orderId);
    }

    @GetMapping("/changeExpiredStatus")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE},required = false)
    public void changeExpiredStatus(){
        bargainOrderService.changeExpiredStatus();
    }

}
